package edu.tongji.engine.db.dao;

import java.util.Set;

import edu.tongji.engine.common.Log;
import edu.tongji.engine.db.HibernateTemplate;
import edu.tongji.engine.db.pojo.Actor;
import edu.tongji.engine.db.pojo.Task;

public class TaskDao implements ITaskDao{

	private HibernateTemplate session;
	private ActorDao actorDao;
	public TaskDao(){
		session = new HibernateTemplate();
		actorDao = new ActorDao();
	}

	public boolean deleteTask(Task task) {
		// TODO Auto-generated method stub
		try{
			session.delete(task);
			return true;
			}
		catch(Exception e){
			System.out.println(e.toString());
			Log.getLoger().loger.error("delete Task Object Fail! ", e);
			return false;
		}
	}

	public boolean deleteTask(Long id) {
		// TODO Auto-generated method stub
		try{
			String hql = "delete Task task where task.id = ?";
			session.deleteById(hql, id);
			return true;
		}catch(Exception e){
			System.out.println(e.toString());
			Log.getLoger().loger.error("delete Task Object Fail! ", e);
			return false;
		}
	}

	public Task getTask(Long id) {
		// TODO Auto-generated method stub
		StringBuilder hql = new StringBuilder("from Task as task where task.id=");
		hql.append(id);
		Task task = (Task)session.findByHQL(hql.toString()).get(0);
		return task;
	}

	public Set<Task> getTasksByActor(String name) {
		// TODO Auto-generated method stub
		Actor actor = actorDao.getActor(name);
		return actor.getTasks();
	}

	public Set<Task> getTasksByActor(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean saveTask(Task task) {
		// TODO Auto-generated method stub
		try{
			session.saveOrUpdate(task);
			return true;
			}
			catch(Exception e){
				System.out.println(e.toString());
				Log.getLoger().loger.error("save Task Object Fail! ", e);
				return false;
			}
	}

	public boolean deleteAllTasks() {
		try{
			String hql = "delete from Task";
			session.deleteByHQL(hql);
			return true;
			}
			catch(Exception e){
				System.out.println(e.toString());
				Log.getLoger().loger.error("delete all Objects Fail! ", e);
				return false;
			}
	}

}
